﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;

namespace SlvPartecipazione
{
    public partial class CircleBar : UserControl, INotifyPropertyChanged
    {
#if DEBUG
        public decimal TempoLimiteInMillisecondi = 10000;
#else
        public decimal TempoLimiteInMillisecondi = 30000;
#endif

        private int intervallo = 100;

        private decimal _value = 0;
        public decimal Value
        {
            get { return _value; }
            set { _value = value; OnPropertyChanged("Value"); }
        }

        private String contatore;
        public String Contatore
        {
            get { return contatore; }
            set
            {
                contatore = value; OnPropertyChanged("Contatore");
            }
        }

        public void Pause()
        {
            _timer.Stop();
        }

        private DispatcherTimer _timer;
        decimal numeroDiCicli;
        decimal incrementaOgniCiclo;

        //internal void Start(MainPage mainPage)
        //{
        //    Start();
        //    mainPage.btnStop.ClickMode = ClickMode.Press;
        //}

        public void Start(MainPage mainPage)
        {
            _timer = new DispatcherTimer();

            if (Value == 0)
            {
                numeroDiCicli = TempoLimiteInMillisecondi / intervallo;

                incrementaOgniCiclo = 100 / numeroDiCicli;
            }

            _timer.Interval = TimeSpan.FromMilliseconds(intervallo);
            _timer.Tick += (_, a) => 
            {
                numeroDiCicli--;

                if (numeroDiCicli < 0)
                {
                    _timer.Stop();
                    Value = 0;
                    // stop della musica e stop dell'intermittenza del testo del pulsante
                    mainPage.Finish();
                    return;
                }
                else
                {
                    Value = Value + incrementaOgniCiclo;
                }

                Contatore = (numeroDiCicli / 10).ToString("00.0") + " sec.";
            };

            _timer.Start();
        }
                
        public CircleBar()
        {
            InitializeComponent();
            
            this.DataContext = this;
        }
        

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #endregion
    }


}
